package com.sinping.qh.api.admin.lab;

import com.sinping.qh.api.app.base.BaseAppEndPoint;
import com.sinping.qh.api.support.*;
import com.sinping.qh.dto.admin.lab.LabInspectItemRelationDto;
import com.sinping.qh.dto.admin.lab.LabInspectItemTreeDto;
import com.sinping.qh.dto.admin.lab.LabOrderDto;
import com.sinping.qh.query.lab.LabInspectItemTreeQuery;
import com.sinping.qh.repository.mongo.lab.LabInspectItemTreeRepository;
import com.sinping.qh.service.admin.lab.ILabInspectItemTreeService;
import com.sinping.qh.utils.mapper.JsonMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author jiahuaiqing
 * @create 2018/6/9
 */
@Api(value = "admin检查项树形管理", description = "admin检查项树形管理")
@RestController
@RequestMapping(name = "admin检查项树形管理", value = Const.ADMIN_URI + "inspectitem/tree")
public class LabInspectItemTreeEndPoint extends BaseAppEndPoint {

    @Autowired
    ILabInspectItemTreeService iLabInspectItemTreeService;

    /**
     * 实验室检查项 一级
     */
    @ApiOperation(httpMethod = "POST", value = "添加一级实验室检查项", consumes = MediaTypes.JSON, notes = "item_name,item_subcategory,item_category必须填写")
    @RequestMapping(method = RequestMethod.POST, name = "添加一级实验室检查项", value = "/one")
    public ResDto addLaboratoryInspectItemOne(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"item_name\":\"供电系统\",<Br>\"item_subcategory\":\"2\",<Br>\"item_category\":\"1\"}")
    @RequestParam(required = true) String msg) {

        try {
            LabInspectItemTreeDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemTreeDto.class);
            if (labInspectItemDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "名称不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemCategory())) {
                return new ResDto(Const.PARAM_ERROR, "类型不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemSubcategory())) {
                return new ResDto(Const.PARAM_ERROR, "分类不为空");
            }
            ResDto reDto = iLabInspectItemTreeService.addLabInspectItemOne(request, labInspectItemDto);
            return reDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 实验室检查项 二级
     */
    @ApiOperation(httpMethod = "POST", value = "添加二级实验室检查项", consumes = MediaTypes.JSON, notes = "item_name,item_parentid,item_category(且只能是1隐患)必须填写")
    @RequestMapping(method = RequestMethod.POST, name = "添加二级实验室检查项", value = "/two")
    public ResDto addLaboratoryInspectItemTwo(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"item_name\":\"责任体系\",<BR>\"item_parentid\":\"9ed1640f86274289a3f1a372dfd8c7d9\",<BR>\"item_category\":\"1\"}")
    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemTreeDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemTreeDto.class);
            if (labInspectItemDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "名称不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemParentId())) {
                return new ResDto(Const.PARAM_ERROR, "上级id不为空");
            }
            ResDto resDto = iLabInspectItemTreeService.addLabInspectItemTwo(request, labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 实验室检查项 三级 隐患描述
     */
    @ApiOperation(httpMethod = "POST", value = "添加三级实验室检查项", consumes = MediaTypes.JSON, notes = "item_name,item_parentid必须填写")
    @RequestMapping(method = RequestMethod.POST, name = "添加三级实验室检查项", value = "/three")
    public ResDto addLaboratoryInspectItemThree(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"item_name\":\"xxxxxx\",\"item_parentid\":\"xxxxxx\"}")
    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemTreeDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemTreeDto.class);
            if (labInspectItemDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "隐患描述不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemParentId())) {
                return new ResDto(Const.PARAM_ERROR, "上级id不为空");
            }
            ResDto resDto = iLabInspectItemTreeService.addLabInspectItemThree(request, labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 获取的检查项列表,分页
     *
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取分页的检查项列表,分页", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.GET, name = "获取分页的检查项列表,分页", value = "/inspectitem")
    public ResDto getLabInspectItemFrontPage(@ApiParam(name = "msg", value = "{\"page_size\":10, \"page_current\":1,\"condition\":{}}")
                                             @RequestParam(required = true) String msg) {
        try {
            FrontPage frontPage = JsonMapper.defaultMapper().fromJson(msg, FrontPage.class);
            if (frontPage == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            ResDto resDto = iLabInspectItemTreeService.getLabInspectItemFrontPage(frontPage);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 获取的检查项列表,不分页
     * 特殊处理直接用LabInspectItemTreeQuery 来接的后台前端数据
     *
     * @param msg
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取检查项列表,不分页", consumes = MediaTypes.JSON, notes = "id,itemParentId,itemName,itemSubcategory,itemLevel,status传值是任意组合,查询啥传啥,{}查询全部")
    @RequestMapping(method = RequestMethod.GET, name = "获取检查项列表,不分页", value = "/inspectitemlist")
    public ResDto getLabInspectItemList(@ApiParam(name = "msg", value = "{\"id\":\"adfsfs\",\"itemParentId\":\"adfsfs\",<br>\"itemName\":\"aaaa\",<br>\"itemSubcategory\":\"0\",\"itemLevel\":\"1\",<br>\"status\":\"0\"}")
                                        @RequestParam(required = true) String msg) {
        try {
            LabInspectItemTreeQuery labInspectItemTreeQuery = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemTreeQuery.class);
            if (labInspectItemTreeQuery == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (labInspectItemTreeQuery.getItemSubcategory().equals("")) {
                labInspectItemTreeQuery.setItemSubcategory(null);
            }
            ResDto resDto = iLabInspectItemTreeService.getLabInspectItemList(labInspectItemTreeQuery);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 获取所有检查项列表,不分页
     *
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取所有检查项列表(一级二级三级树结构),不分页", consumes = MediaTypes.JSON, notes = "itemSubcategory的分类集合，必传。[]：表示查询全部")
    @RequestMapping(method = RequestMethod.GET, name = "获取所有检查项列表(一级二级三级树结构),不分页", value = "/allinspectitemlist")
    public ResDto getAllLabInspectItemList(@ApiParam(name = "msg", value = "[\"0\",\"1\",\"2\"]", required = true)
                                           @RequestParam(required = true) String msg) {
        try {
            List<String> itemSubcategoryList = JsonMapper.defaultMapper().fromJson(msg, JsonMapper.defaultMapper().buildCollectionType(List.class, String.class));
            if (null == itemSubcategoryList) {
                logger.error("itemSubcategoryList分类集合不能为空");
                return new ResDto(Const.PARAM_ERROR, "itemSubcategoryList分类集合不能为空");
            }
            ResDto resDto = iLabInspectItemTreeService.getAllLabInspectItemList(itemSubcategoryList);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    /**
     * 获取所有危险源隐患库列表,不分页
     *
     * @return
     */
    @ApiOperation(httpMethod = "GET", value = "获取所有危险源隐患库列表,不分页", consumes = MediaTypes.JSON, notes = "id,itemName,itemSubcategory,itemLevel,status传值是任意组合,查询啥传啥,{}查询全部")
    @RequestMapping(method = RequestMethod.GET, name = "获取所有危险源隐患库列表,不分页", value = "/alldangerlist")
    public ResDto getLabDangerList() {
        try {
            ResDto resDto = iLabInspectItemTreeService.getAllLabDangerList();
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    @ApiOperation(httpMethod = "PUT", value = "修改检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.PUT, name = "修改检查项", value = "/inspectitem")
    public ResDto updateLabInspectItem(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"_id\":\"xxxxxx\",\"item_name\":\"xxxxxx\",<Br>可空\"item_subcategory\":\"xxxxxx\",<Br>可空\"item_category\":\"xxxx\"}")
    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemTreeDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemTreeDto.class);
            if (labInspectItemDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getId())) {
                return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getItemName())) {
                return new ResDto(Const.PARAM_ERROR, "名称不为空");
            }
            ResDto resDto = iLabInspectItemTreeService.updateLabInspectItem(request, labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    @Autowired
    LabInspectItemTreeRepository labInspectItemRepository;

    /**
     * 删除检查项
     *
     * @param id
     * @return
     */
    @ApiOperation(httpMethod = "DELETE", value = "删除检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.DELETE, name = "删除检查项", value = "/inspectitem/{id}")
    public ResDto deleteLabInspectItemById(@ApiParam(name = "id", value = "5b0e7a300a4145204c48768c")
                                           @PathVariable String id) {
        if (!StringUtils.isNotBlank(id)) {
            return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
        }
        ResDto resDto = iLabInspectItemTreeService.deleteLabInspectItemById(id);
        return resDto;
    }


    /**
     * （配置检查项）添加--实验室配置检查项
     */
    @ApiOperation(httpMethod = "POST", value = "实验室配置检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.POST, name = "实验室配置检查项", value = "/labtoinspectitem")
    public ResDto addLaboratoryInspectItemRelation(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"lab_id\":\"xxxxxx\",\"inspectItem_ids\":<Br>[\"inspectItem_id\",\"inspectItem_id\"]}")
    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemRelationDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemRelationDto.class);
            if (labInspectItemDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getLabId())) {
                return new ResDto(Const.PARAM_ERROR, "实验室id不为空");
            }
            if (labInspectItemDto.getInspectItemIds().isEmpty()) {
                return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
            }
            ResDto resDto = iLabInspectItemTreeService.addLaboratoryInspectItemRelation(request, labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    /**
     * （配置检查项）删除实验室已经配置的检查项
     */
    @ApiOperation(httpMethod = "DELETE", value = "实验室配置检查项", consumes = MediaTypes.JSON)
    @RequestMapping(method = RequestMethod.DELETE, name = "实验室配置检查项", value = "/labtoinspectitem")
    public ResDto deleteLaboratoryInspectItemRelation(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"lab_id\":\"xxxxxx\",\"inspectItem_ids\":<Br>[\"inspectItem_id\",\"inspectItem_id\"]}")
    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemRelationDto labInspectItemDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemRelationDto.class);
            if (labInspectItemDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemDto.getLabId())) {
                return new ResDto(Const.PARAM_ERROR, "实验室id不为空");
            }
            if (labInspectItemDto.getInspectItemIds().isEmpty()) {
                return new ResDto(Const.PARAM_ERROR, "检查项id不为空");
            }
            ResDto resDto = iLabInspectItemTreeService.deleteLaboratoryInspectItemRelation(request, labInspectItemDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    /**
     * 根据实验室id,获取--实验室已经配置的检查项
     */
    @ApiOperation(httpMethod = "GET", value = "根据实验室id,获取实验室已经配置的检查项", consumes = MediaTypes.JSON, notes = "id是实验室id")
    @RequestMapping(method = RequestMethod.GET, name = "根据实验室id,获取实验室已经配置的检查项", value = "/labtoinspectitem/{id}")
    public ResDto getLaboratoryInspectItemRelation(HttpServletRequest request, @ApiParam(name = "id", value = "xxxxxx") @PathVariable String id) {
        try {
            if (!StringUtils.isNotBlank(id)) {
                return new ResDto(Const.PARAM_ERROR, "实验室id不为空");
            }
            ResDto resDto = iLabInspectItemTreeService.getLaboratoryInspectItemRelation(request, id);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

    /**
     * 获取--实验室已经配置的检查项中的隐患描述
     */
    @ApiOperation(httpMethod = "GET", value = "获取实验室已经配置的检查项中的隐患描述", consumes = MediaTypes.JSON, notes = "item_subcategory： 0 院系级 1实验室级")
    @RequestMapping(method = RequestMethod.GET, name = "获取实验室已经配置的检查项中的隐患描述", value = "/labinspectitemhiddendanger")
    public ResDto getLabInspectItemDescRelation(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"item_subcategory\":\"xxxxxx\",\"lab_id\":\"XXXXXXXX\"}")
    @RequestParam(required = true) String msg) {
        try {
            LabInspectItemTreeDto labInspectItemTreeDto = JsonMapper.defaultMapper().fromJson(msg, LabInspectItemTreeDto.class);
            if (labInspectItemTreeDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labInspectItemTreeDto.getItemSubcategory())) {
                return new ResDto(Const.PARAM_ERROR, "分类不为空");
            }
            ResDto resDto = iLabInspectItemTreeService.getLabInspectItemDescRelation(request, labInspectItemTreeDto.getItemSubcategory(), labInspectItemTreeDto.getLabId());
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }


    /**
     * 检查项的三级隐患描述--排序
     */
    @ApiOperation(httpMethod = "PUT", value = "检查项的三级隐患描述--排序", consumes = MediaTypes.JSON, notes = "before:要排序的前一个item_order,after是后一个item_order")
    @RequestMapping(method = RequestMethod.PUT, name = "检查项的三级隐患描述--排序", value = "/order")
    public ResDto putLabInspectItemDesc(HttpServletRequest request, @ApiParam(name = "msg", value = "{\"before\":\"xxxxxx\",\"after\":\"XXXXXXXX\"}")
    @RequestParam(required = true) String msg) {
        try {
            LabOrderDto labOrderDto = JsonMapper.defaultMapper().fromJson(msg, LabOrderDto.class);
            if (labOrderDto == null) {
                logger.error("参数为空或有不正确的数据格式");
                return new ResDto(Const.PARAM_ERROR, "参数为空或有不正确的数据格式");
            }
            if (!StringUtils.isNotBlank(labOrderDto.getAfter().toString())) {
                return new ResDto(Const.PARAM_ERROR, "after不能为空");
            }
            if (!StringUtils.isNotBlank(labOrderDto.getBefore().toString())) {
                return new ResDto(Const.PARAM_ERROR, "before不能为空");
            }
            ResDto resDto = iLabInspectItemTreeService.putLabInspectItemDescOrder(request, labOrderDto);
            return resDto;
        } catch (Exception e) {
            e.printStackTrace();
            return new ResDto(Const.FORMAT_ERROR, "Json数据解析异常");
        }
    }

}
